/*
 * Copyright 2014 Cungmua, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cungmua.vn.kiosk.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cungmua.vn.kiosk.AbstractActivity;
import cungmua.vn.kiosk.R;
import cungmua.vn.kiosk.activity.DangGiaoActivity.getDonHang;
import cungmua.vn.kiosk.db.DatabaseHandler;
import cungmua.vn.kiosk.handle.NhanVienHandler;
import cungmua.vn.kiosk.handle.ServiceHandler;
import cungmua.vn.kiosk.model.NhanVien;
import cungmua.vn.kiosk.network.CallServices;
import cungmua.vn.kiosk.tabs.TabGroupActivity;
import cungmua.vn.kiosk.utils.Sharereferences;
import cungmua.vn.kiosk.utils.WriteLog;

/**
 * @author kelvin.nguyen
 * 
 */
public class NhanVienActivity extends AbstractActivity {
	private ProgressDialog dialog;

	private EditText ma_nhanvien;

	private EditText matkhau;

	private Button btnLogin;

	private String pin, passwrd;

	private NhanVien nhanvien;
	private DatabaseHandler db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nhanvien);
		db = new DatabaseHandler(self);
		initView();
	}

	public void initView() {
		ma_nhanvien = (EditText) findViewById(R.id.editTextMa);
		matkhau = (EditText) findViewById(R.id.editTextMatKhau);
		btnLogin = (Button) findViewById(R.id.btn_login);
		btnLogin.setOnClickListener(onclick);
	}

	public class checkLogin extends AsyncTask<Void, Void, Integer> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {

			super.onPreExecute();
			dialog = new ProgressDialog(getParent());
			dialog.setMessage(getResources().getString(R.string.label_dialog));
			dialog.show();

		}

		@Override
		protected Integer doInBackground(Void... params) {
			String json = CallServices.checkLogin(self, pin, passwrd);

			ServiceHandler service = new ServiceHandler();
			NhanVienHandler nhanVienHandler = service.getNhanVien(json);
			nhanvien = nhanVienHandler.getData();
			return nhanVienHandler.getCode();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if (dialog.isShowing()) {
				dialog.cancel();
			}
			switch (result) {
			case 0:// successfull
					// save info nhanvien localhost

				db.addNhanvien(nhanvien);

				// save token for other place reused access server get data
				Sharereferences.saveToken(nhanvien.getToken(), self);

				// go to Screen Nhan Vien Detail Settings
				Intent intent_nhanvien_detail = new Intent(self,
						NhanVienDetailActivity.class);
				TabGroupActivity tabGroupActivity = (TabGroupActivity) getParent();
				tabGroupActivity.startChildActivity("NhanVienDetailActivity",
						intent_nhanvien_detail);
				break;
			case 1:// account locked
				AlertDialog alert = getAlertDialog(getParent(), getResources()
						.getString(R.string.dangnhap), getResources()
						.getString(R.string.msg_clocked));
				alert.show();
				break;
			case 2:// account not exist
				AlertDialog alert_exist = getAlertDialog(getParent(),
						getResources().getString(R.string.dangnhap),
						getResources().getString(R.string.msg_not_exits));
				alert_exist.show();
				break;
			case 3:// success . Please change password for first login
					// save info nhanvien localhost
				db.addNhanvien(nhanvien);
				// go to Screen Nhan Vien Detail Settings
				Intent intent_change_password= new Intent(self,
						ChangePassword.class);
				TabGroupActivity tabGroupActivityChangePassword = (TabGroupActivity) getParent();
				tabGroupActivityChangePassword.startChildActivity("ChangePassword",
						intent_change_password);

				break;
			case 1000:// data not invalid
				AlertDialog alert_invalid = getAlertDialog(getParent(),
						getResources().getString(R.string.dangnhap),
						getResources().getString(R.string.msg_data_invalid));
				alert_invalid.show();
				break;
			case 1001:// System error
				AlertDialog alert_system = getAlertDialog(getParent(),
						getResources().getString(R.string.dangnhap),
						getResources().getString(R.string.msg_system_error));
				alert_system.show();
				break;
			default:
				break;
			}

		}

	}

	public OnClickListener onclick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_login:
				pin = ma_nhanvien.getText().toString();
				passwrd = matkhau.getText().toString();

				if (pin.length() < 1) {
					ma_nhanvien
							.setError(Html
									.fromHtml("<font color='red'>Vui lòng nhập mã nhân viên</font>"));
					return;
				}
				if (passwrd.length() < 1) {
					ma_nhanvien
							.setError(Html
									.fromHtml("<font color='red'>Vui lòng nhập mật khẩu</font>"));
					return;
				}
				if(isNetworkAvailable()){
					new checkLogin().execute();
				}else{
					showToast(getResources().getString(R.string.network_unable), Toast.LENGTH_LONG);return;
				}
				
				break;

			default:
				break;
			}

		}
	};

	protected void onResume() {
		super.onResume();
		if(Sharereferences.getToken(self)!=null){

			// go to Screen Nhan Vien Detail Settings
			Intent intent_nhanvien_detail = new Intent(self,
					NhanVienDetailActivity.class);
			TabGroupActivity tabGroupActivity = (TabGroupActivity) getParent();
			tabGroupActivity.startChildActivity("NhanVienDetailActivity",
					intent_nhanvien_detail);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (dialog != null) {
			dialog.dismiss();
		}
		db.close();
	}

}
